//
//  IntervalTimer.m
//  IntervalTimer
//


#import "IntervalTimer.h"


@implementation IntervalTimer


+ (void)initialize {	
	//whenever workTimeMinutes and workTimeSeconds get updated, workInterval's binding should get updated too
	[IntervalTimer setKeys:
		[NSArray arrayWithObjects:@"workTimeMinutes", @"workTimeSeconds", nil] 
		triggerChangeNotificationsForDependentKey:@"attributedStringOfWorkInterval"];
}

- (id) init {
	self = [super init];
	workTimeMinutes = 1;
	restTimeMinutes = 1;
	numSets = 5;
	workText = @"work phase, set $set";
	restText = @"rest phase, set $set";
	
	return self;
}
- (int) workTimeMinutes {
	return workTimeMinutes;
}
- (int) workTimeSeconds{
	return workTimeSeconds;
}
- (int) restTimeMinutes{
	return restTimeMinutes;
}
- (int) restTimeSeconds{
	return restTimeSeconds;
}
- (int) numSets{
	return numSets;
}

- (NSString *) workText{
	return workText;
}
- (NSString *) restText{
	return restText;
}



- (void) setWorkTimeMinutes: (int) val {
	workTimeMinutes = val;
}
- (void) setRestTimeMinutes: (int) val {
	restTimeMinutes = val;
}
- (void) setWorkTimeSeconds: (int) val {
	workTimeSeconds = val;
}
- (void) setRestTimeSeconds: (int) val {
	restTimeSeconds = val;
}
- (void) setNumSets: (int) val {
	numSets = val;
}

- (void) setWorkText: (NSString *) val {
	NSString * oldval = workText;
	workText = [val retain];
	[oldval release];
}
- (void) setRestText: (NSString *) val{
	NSString * oldval = restText;
	restText = [val retain];
	[oldval release];
}

- (int) workInterval {
	return workTimeMinutes * 60 + workTimeSeconds;
}

- (int) restInterval {
	return restTimeMinutes * 60 + restTimeSeconds;
}

- (NSAttributedString *) attributedStringOfWorkInterval {
	
	
	int interval = workTimeMinutes * 60 + workTimeSeconds;
	
	//should be consistent with the one used in -[IntervalTimerController awakeFromNib]
	NSFont * theFont = [NSFont systemFontOfSize:80];
	NSAttributedString *attrStr = [[NSAttributedString alloc] 
								   initWithString:[NSString stringWithFormat:@"%d", interval]
								   attributes:[NSDictionary dictionaryWithObjectsAndKeys:theFont, NSFontAttributeName, nil]];
	[attrStr autorelease];
	
	return attrStr;
}

- (void)setNilValueForKey:(NSString *)key {
	//handle it when you hit backspace in one of the textfields and it nulls it out and tries to insert that into an int
	//set it to 0 instead.
	[self setValue:[NSNumber numberWithInt:1] forKey:key];
	
}

-(void) dealloc {
	[workText release];
	[restText release];
	[super dealloc];
}



@end
